package com.kaiyidiannao.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import com.kaiyidiannao.activity.user_detail.OnCustomerListener;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.TourBean;
import com.kaiyidiannao.model.req.model.GetCloseOpen;
import com.kaiyidiannao.view.ScaleImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by david on 14-4-22.
 * 凑队去 - 召集
 */
public class MyTourGatherAdapter extends ImageLoaderBaseAdapter {
    private ImageLoader imageLoader;
    private LayoutInflater layoutInflater;
    private List<TourBean> list = new ArrayList<TourBean>();
    private String[] imgs;
    private Context context;
    private OnCustomerListener listener;
    private int tip = 0;

    public MyTourGatherAdapter(Context context, List<TourBean> list, int tip) {
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        this.list = list;
        this.layoutInflater = LayoutInflater.from(context);
        this.context = context;
        this.tip = tip;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;

        if (DataCenter.lmap1.get(position) == null) {
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.my_tour_gether, null);
            viewHolder.imageView = (ScaleImageView) convertView.findViewById(R.id.tour_small_note_item_ImageView);
            viewHolder.titleTextview = (TextView) convertView.findViewById(R.id.my_tour_small_note_nameTextView);
            viewHolder.peppleTextView = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.myGatherItemButton = (ImageButton) convertView.findViewById(R.id.myGatherItemButton);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final TourBean bean = list.get(position);
        imageLoader.displayImage(bean.getThumb(), viewHolder.imageView, options);
        viewHolder.titleTextview.setText(bean.getTitle());
        viewHolder.peppleTextView.setText(bean.getTotal() + "人" + "  " + "点击查看");

        if ("2".equals(bean.getStatus())) {
            viewHolder.myGatherItemButton.setImageResource(R.drawable.mypublish_dark_button);
        } else {
            viewHolder.myGatherItemButton.setImageResource(R.drawable.mypublish_light_button);
        }

        viewHolder.myGatherItemButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!=null){
                    listener.onCustomerListener(v, position);
                }
            }
        });

        viewHolder.peppleTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!=null){
                    listener.onCustomerListener(v,position);
                }
            }
        });

        return convertView;
    }

    /**
     * 设置适配器上，某个控件的监听事件
     *
     * @param listener
     */
    public void setOnCustomListener(OnCustomerListener listener) {
        this.listener = listener;
    }

    final static class ViewHolder {
        ScaleImageView imageView;
        TextView titleTextview, peppleTextView;
        ImageButton myGatherItemButton;
    }

}
